Skip to content

DRAIN_EXCLUSIVE_ACTIONS implementation #1355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2025

Conversation

steve-the-edwards
Copy link
Contributor

Add an option to get the next action that is mutually exclusive to the dirty nodes. Then we can apply as many of those actions as possible.

Add tests for this as well and ensure existing tests can run with this runtime config on.

@steve-the-edwards steve-the-edwards force-pushed the sedwards/dea-2-for-real branch 4 times, most recently from b276a5b to eefc6d8 Compare June 20, 2025 20:56
@steve-the-edwards steve-the-edwards marked this pull request as ready for review June 20, 2025 20:57
Copy link
Collaborator

@zach-klippenstein zach-klippenstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the interaction between conflation and drainExclusive. It looks like we run the drainExclusive loop first, then render, then conflate, drainExclusive, and render, etc. Wouldn't we want to continuously interleave drainExclusive and conflation passes? Or is that what we're doing and I just can't read code?

): Unit = Unit

public sealed interface RuntimeLoopOutcome
public sealed interface RuntimeUpdate
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the risk of over-optimizing, this could be a value class to avoid allocating RenderingProduced objects every pass even when they're ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. will take a look at doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to talk more with you about what you are thinking. I am not sure we even need to report the rendering itself to the interceptor. I can start by just removing that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that object is already allocated (probably why I was not worried originally). So we could start by just making RenderingProduced a value class I think I was confused originally because this comment was on the RuntimeUpdate sealed interface

Copy link
Contributor Author

@steve-the-edwards steve-the-edwards Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made RenderingProduced a value class now. The others are just objects, so this should all be quite fast / have minimal allocations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no point in making a subclass of this a value class, since it will be boxed when referred to as a parent type. We'd need to replace the entire sealed class/interface hierarchy with a single value class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, interesting!

}

@Test
fun `for_drain_exclusive_and_render_only_when_state_changes_we_handle_multiple_actions_in_one_render_but_we_do_pass_rendering_if_state_changed_earlier`() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You shouldn't need the backticks here, you're not using spaces or other illegal characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually because this is KMP code that runs on js as well, IIRC. For some reason the kotlin-js compiler can't handle long names that don't have ticks? Or maybe that's why I had to add the _. Now I can't remember. But it was something with kotlin-js.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out I was just remembering wrong or had copied this forward when not needed. Removed now!

@steve-the-edwards steve-the-edwards force-pushed the sedwards/dea-2-for-real branch from e6b2344 to 1551901 Compare June 24, 2025 14:18
@steve-the-edwards steve-the-edwards requested a review from a team as a code owner July 3, 2025 14:23
@steve-the-edwards steve-the-edwards force-pushed the sedwards/dea-2 branch 2 times, most recently from 5b45b9a to 44397aa Compare July 3, 2025 19:24
@steve-the-edwards steve-the-edwards force-pushed the sedwards/dea-2-for-real branch 2 times, most recently from 9a60fe3 to 0acd80d Compare July 3, 2025 19:44
@steve-the-edwards steve-the-edwards force-pushed the sedwards/dea-2-for-real branch from 0acd80d to 4d41e92 Compare July 3, 2025 19:47
@steve-the-edwards
Copy link
Contributor Author

I don't understand the interaction between conflation and drainExclusive. It looks like we run the drainExclusive loop first, then render, then conflate, drainExclusive, and render, etc. Wouldn't we want to continuously interleave drainExclusive and conflation passes? Or is that what we're doing and I just can't read code?

the main loop (after the first synchronous render) is:

  1. awaitAndApplyAction() for the first available action
  2. DEA loop: Drain any exclusive actions immediately available - synchronously for any actions already queued (no suspend here).
  3. render()
  4. CSR loop: Drain any other actions immediately available; render() after each one.
  5. pass updated rendering down the StateFlow.

No interleaving is necessary from what I can tell. The exclusive actions for DEA are just a subset of the actions that can be applied without rendering at all.

Base automatically changed from sedwards/dea-2 to main July 3, 2025 20:56
@steve-the-edwards steve-the-edwards force-pushed the sedwards/dea-2-for-real branch 2 times, most recently from b0dfc9c to 3635e7d Compare July 4, 2025 14:38
@@ -281,7 +296,7 @@ internal class WorkflowNode<PropsT, StateT, OutputT, RenderingT>(

if (!runtimeConfig.contains(PARTIAL_TREE_RENDERING) ||
!lastRendering.isInitialized ||
subtreeStateDidChange
subtreeStateDirty

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be checking the selfStateDirty here as well?
is there in which the self state might be dirty but the subtree isnt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. if self is dirty, subtree is also dirty. Not true the other way around though, subtree may be dirty without the self being dirty.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subtree includes self, basically.

@rjrjr
Copy link
Contributor

rjrjr commented Jul 7, 2025

I don't understand the interaction between conflation and drainExclusive. It looks like we run the drainExclusive loop first, then render, then conflate, drainExclusive, and render, etc. Wouldn't we want to continuously interleave drainExclusive and conflation passes? Or is that what we're doing and I just can't read code?

the main loop (after the first synchronous render) is:

  1. awaitAndApplyAction() for the first available action
  2. DEA loop: Drain any exclusive actions immediately available - synchronously for any actions already queued (no suspend here).
  3. render()
  4. CSR loop: Drain any other actions immediately available; render() after each one.
  5. pass updated rendering down the StateFlow.

No interleaving is necessary from what I can tell. The exclusive actions for DEA are just a subset of the actions that can be applied without rendering at all.

It'd be good to capture this in a comment in the code.

@zach-klippenstein
Copy link
Collaborator

I don't understand the interaction between conflation and drainExclusive. It looks like we run the drainExclusive loop first, then render, then conflate, drainExclusive, and render, etc. Wouldn't we want to continuously interleave drainExclusive and conflation passes? Or is that what we're doing and I just can't read code?

the main loop (after the first synchronous render) is:

  1. awaitAndApplyAction() for the first available action
  2. DEA loop: Drain any exclusive actions immediately available - synchronously for any actions already queued (no suspend here).
  3. render()
  4. CSR loop: Drain any other actions immediately available; render() after each one.
  5. pass updated rendering down the StateFlow.

No interleaving is necessary from what I can tell. The exclusive actions for DEA are just a subset of the actions that can be applied without rendering at all.

Why would exclusive actions only be available after (1) and not after (3)?

@steve-the-edwards
Copy link
Contributor Author

steve-the-edwards commented Jul 7, 2025

I don't understand the interaction between conflation and drainExclusive. It looks like we run the drainExclusive loop first, then render, then conflate, drainExclusive, and render, etc. Wouldn't we want to continuously interleave drainExclusive and conflation passes? Or is that what we're doing and I just can't read code?

the main loop (after the first synchronous render) is:

  1. awaitAndApplyAction() for the first available action
  2. DEA loop: Drain any exclusive actions immediately available - synchronously for any actions already queued (no suspend here).
  3. render()
  4. CSR loop: Drain any other actions immediately available; render() after each one.
  5. pass updated rendering down the StateFlow.

No interleaving is necessary from what I can tell. The exclusive actions for DEA are just a subset of the actions that can be applied without rendering at all.

Why would exclusive actions only be available after (1) and not after (3)?

We chatted offline and realized I need to make a few things much more clear in the code:

  • we only have new actions to process when we suspend which only happens with 1 .
  • the DEA loop will do nothing when using an immediate dispatcher where the select will return 'immediately' after the first action is available. We need to use a fairer dispatcher to get multiple actions to drain.
  • We don't need to run the DEA loop after 4 (after CSR action handling) again because there was no suspension there so there are no new DEA actions ready to be processed.

@steve-the-edwards steve-the-edwards force-pushed the sedwards/dea-2-for-real branch 2 times, most recently from 83c12df to 176b598 Compare July 9, 2025 20:32
@steve-the-edwards steve-the-edwards force-pushed the sedwards/dea-2-for-real branch from 176b598 to e48d80e Compare July 11, 2025 14:28
@steve-the-edwards steve-the-edwards merged commit 7285d9a into main Jul 11, 2025
64 checks passed
@steve-the-edwards steve-the-edwards deleted the sedwards/dea-2-for-real branch July 11, 2025 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants